02. Automatic Memory Allocation (The Stack)
ND213 C03 L02 02.1 Automatic Memory Allocation (The Stack) HS
Properties of Stack Memory
In the available literature on C++, the terms stack and heap are used regularly, even though this is not formally correct: C++ has the free space , storage classes and the storage duration of objects. However, since stack and heap are widely used in the C++ community, we will also use it throughout this course. Should you come across the above-mentioned terms in a book or tutorial on the subject, you now know that they refer to the same concepts as stack and heap do.
As mentioned in the last section, the stack is the place in virtual memory where the local variables reside, including arguments to functions. Each time a function is called, the stack grows (from top to bottom) and each time a function returns, the stack contracts. When using multiple threads (as in concurrent programming), it is important to know that each thread has its own stack memory - which can be considered thread-safe.
In the following, a short list of key properties of the stack is listed:
-
The stack is a contiguous block of memory . It will not become fragmented (as opposed to the heap) and it has a fixed maximum size.
-
When the maximum size of the stack memory is exceeded, a program will crash.
-
Allocating and deallocating memory is fast on the stack. It only involves moving the stack pointer to a new position.
The following diagram shows the stack memory during a function call:
In the example, the variable
x
is created on the stack within the scope of main. Then, a stack frame which represents the function
Add
and its variables is pushed to the stack, moving the stack pointer further downwards. It can be seen that this includes the local variables
a
and
b
, as well as the return address, a base pointer and finally the return value
s
.
In the following, let us dig a little more deeply and conduct some experiments with variables on the stack.
Stack Growth and Contraction
ND213 C03 L02 02.2 Automatic Memory Allocation (The Stack) SC
Workspace
This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity , so you may be able to download them there.
Workspace Information:
- Default file path:
- Workspace type: jupyter-lab
- Opened files (when workspace is loaded): n/a
Outro
ND213 C03 L02 02.3 Automatic Memory Allocation (The Stack) HS
Before we take a look at the heap memory in the next lesson, let us briefly revisit the principles of call-by-value and call-by-reference with regard to stack usage.